package top.csy8888.ch02.controller;

import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.csy8888.ch02.common.MinioOptions;
import top.csy8888.ch02.vo.ResultVO;

import java.io.InputStream;

/**
 * @author CSY
 * @date 2025 - 09 - 04
 */
@RestController
@AllArgsConstructor
public class UploadController extends BaseController {
    private MinioOptions options;

    @PostMapping("/upload/{path}")
    public ResultVO upload(@PathVariable("path") String path,
                           @RequestParam("file") MultipartFile[] files) throws Exception {
        for (MultipartFile file : files) {
            //获取文件名
            String filename = file.getOriginalFilename();
            //获取文件类型
            String contentType = file.getContentType();
            //获取文件大小
            long size = file.getSize();
            //获取文件输入流
            InputStream in = file.getInputStream();
            //上传到minio的文件服务器
            options.upload(path + "/" + filename,contentType,size,in);
        }
        return success();
    }

}
